/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Services;

import au.com.bytecode.opencsv.CSVWriter;
import com.ControlEscolar.Beans.Exam;
import com.ControlEscolar.Beans.ExamReport;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.ExamReportDAO;
import com.ControlEscolar.Exceptions.ServiceExp;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author listico
 */
public class ExamReportService {

    ExamReportDAO examReport=new ExamReportDAO();
    private static final String EXAM_ADREES_FILE="Downloads/ExamReport.csv";
    ExamService examService=new ExamService();

    public ArrayList<ExamReport> getExamReportList() throws ServiceExp {
        try{
        return examReport.query();
        }
        catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public void writeExamFileReport(String[] ID_Exams) throws ServiceExp{
        try {
            List<String[]> ExamReportDataList=fillDataExamReport(getExams(ID_Exams));
            
            CSVWriter writer = new CSVWriter(new FileWriter(EXAM_ADREES_FILE));

            writer.writeAll(ExamReportDataList);

            writer.close();

        } catch (IOException ex) {
             throw new ServiceExp();
        } 

    }

    private ArrayList<Exam> getExams(String[] ID_Exams){

        ArrayList<Exam> exams=new ArrayList();

        for (int index=0;index<ID_Exams.length;index++){
            exams.add(examService.getExam(ID_Exams[index]));
        }
        return exams;
    }

    private List<String[]> fillDataExamReport(ArrayList<Exam> exams) throws ServiceExp{

        List<String[]> ExamReportDataList=new ArrayList();
    
        ExamReportDataList.add(fillColumnsReportData());

            try {
                for (int index=0;index<exams.size();index++){
                String[] ExamReportData = new String[8];
                ExamReportData[0] = "" + index;
                ExamReportData[1] = exams.get(index).getStudentKey();
                ExamReportData[2] = "" + exams.get(index).getID_Curriculum();
                ExamReportData[3] = exams.get(index).getSubjectKey();
                ExamReportData[4] = exams.get(index).getExamType();
                if (exams.get(index).getExamGrade() > 60) {
                    ExamReportData[5] = "SI";
                } else {
                    ExamReportData[5] = "NO";
                }
                
                ExamReportData[6] = "" + exams.get(index).getExamGrade();
                ExamReportData[7] = "" + examReport.OpportunityNumbers(ExamReportData);

                ExamReportDataList.add(ExamReportData);
            }
            } catch (DAOException ex) {
                throw new ServiceExp();
            }

            return ExamReportDataList;
    }

    private String[] fillColumnsReportData(){
        return new String[]{"Numero","Mátrícula","Clave del programa",
            "Clave de la asignatura","Tipo de examen","Aprobado","Calificacion","Oportunidad"};
    }
}
